package com.project.pudongdata.controller;


import com.project.dto.R;
import com.project.pudongdata.dto.SearchDto;
import com.project.pudongdata.service.PdDisasterRecoveryService;
import com.project.pudongdata.util.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/pdDisasterRecovery")
public class PdDisasterRecoveryController {

    @Resource
    private PdDisasterRecoveryService pdDisasterRecoveryService;

    /**
     * 基础线路数据
     * @param searchDto
     * @return
     * @throws Exception
     */
    @RequestMapping("/lineBaseData")
    public R lineBaseData(@RequestBody SearchDto searchDto) throws Exception {
        String checkCode = checkCode(searchDto);
        if(!"success".equals(checkCode)) {
            return R.fail(checkCode);
        }
        //验证日期
       String vildateSearchDate = vildateSearchDate(searchDto.getSearchDate());
        if(!"success".equals(vildateSearchDate)){
            return R.fail(vildateSearchDate);
        }

        return R.success(pdDisasterRecoveryService.selectTXianluList(searchDto));
    }


    /**
     * 线路班次数据
     * @param searchDto
     * @return
     * @throws Exception
     */
    @RequestMapping("/lineClassesData")
    public R lineClassesData(@RequestBody SearchDto searchDto) throws Exception {
        String checkCode = checkCode(searchDto);
        if(!"success".equals(checkCode)) {
            return R.fail(checkCode);
        }
        //验证日期
        String vildateSearchDate = vildateSearchDate(searchDto.getSearchDate());
        if(!"success".equals(vildateSearchDate)){
            return R.fail(vildateSearchDate);
        }

        return R.success(pdDisasterRecoveryService.selectTXlBcList(searchDto));
    }


    /**
     * 企业班次数据
     * @param searchDto
     * @return
     * @throws Exception
     */
    @RequestMapping("/companyClassesData")
    public R companyClassesData(@RequestBody SearchDto searchDto) throws Exception {
        String checkCode = checkCode(searchDto);
        if(!"success".equals(checkCode)) {
            return R.fail(checkCode);
        }
        //验证日期
        String vildateSearchDate = vildateSearchDate(searchDto.getSearchDate());
        if(!"success".equals(vildateSearchDate)){
            return R.fail(vildateSearchDate);
        }

        return R.success(pdDisasterRecoveryService.selectTQyBcList(searchDto));
    }


    /**
     * 线路首末班发车准点数据
     * @param searchDto
     * @return
     * @throws Exception
     */
    @RequestMapping("/lineDepartTime")
    public R lineDepartTime(@RequestBody SearchDto searchDto) throws Exception {
        String checkCode = checkCode(searchDto);
        if(!"success".equals(checkCode)) {
            return R.fail(checkCode);
        }
        //验证日期
        String vildateSearchDate = vildateSearchDate(searchDto.getSearchDate());
        if(!"success".equals(vildateSearchDate)){
            return R.fail(vildateSearchDate);
        }

        return R.success(pdDisasterRecoveryService.selectTXlShoumobanfczdList(searchDto));
    }

    /**
     * 企业首末班发车准点数据
     * @param searchDto
     * @return
     * @throws Exception
     */
    @RequestMapping("/companyDepartTimeData")
    public R companyDepartTimeData(@RequestBody SearchDto searchDto) throws Exception {
        String checkCode = checkCode(searchDto);
        if(!"success".equals(checkCode)) {
            return R.fail(checkCode);
        }
        //验证日期
        String vildateSearchDate = vildateSearchDate(searchDto.getSearchDate());
        if(!"success".equals(vildateSearchDate)){
            return R.fail(vildateSearchDate);
        }

        return R.success(pdDisasterRecoveryService.selectTQyShoumobanfczdList(searchDto));
    }

    /**
     * 线路起讫站发车准点数据
     * @param searchDto
     * @return
     * @throws Exception
     */
    @RequestMapping("/lineStartEndDepartTimeData")
    public R lineStartEndDepartTimeData(@RequestBody SearchDto searchDto) throws Exception {
        String checkCode = checkCode(searchDto);
        if(!"success".equals(checkCode)) {
            return R.fail(checkCode);
        }
        //验证日期
        String vildateSearchDate = vildateSearchDate(searchDto.getSearchDate());
        if(!"success".equals(vildateSearchDate)){
            return R.fail(vildateSearchDate);
        }

        return R.success(pdDisasterRecoveryService.selectTXlQiqizhanfczdList(searchDto));
    }

    /**
     * 企业起讫站发车准点数据
     * @param searchDto
     * @return
     * @throws Exception
     */
    @RequestMapping("/companyStartEndDepartTimeData")
    public R companyStartEndDepartTimeData(@RequestBody SearchDto searchDto) throws Exception {
        String checkCode = checkCode(searchDto);
        if(!"success".equals(checkCode)) {
            return R.fail(checkCode);
        }
        //验证日期
        String vildateSearchDate = vildateSearchDate(searchDto.getSearchDate());
        if(!"success".equals(vildateSearchDate)){
            return R.fail(vildateSearchDate);
        }

        return R.success(pdDisasterRecoveryService.selectTQyQiqizhanfczdList(searchDto));
    }



    /**
     * 线路挂牌线路发车准点数据
     * @param searchDto
     * @return
     * @throws Exception
     */
    @RequestMapping("/lineListingLineDepartTimeData")
    public R lineListingLineDepartTimeData(@RequestBody SearchDto searchDto) throws Exception {
        String checkCode = checkCode(searchDto);
        if(!"success".equals(checkCode)) {
            return R.fail(checkCode);
        }
        //验证日期
        String vildateSearchDate = vildateSearchDate(searchDto.getSearchDate());
        if(!"success".equals(vildateSearchDate)){
            return R.fail(vildateSearchDate);
        }

        return R.success(pdDisasterRecoveryService.selectTXlGuapaifczdList(searchDto));
    }

    /**
     * 企业挂牌线路发车准点数据
     * @param searchDto
     * @return
     * @throws Exception
     */
    @RequestMapping("/companyListingLineDepartTimeData")
    public R companyListingLineDepartTimeData(@RequestBody SearchDto searchDto) throws Exception {
        String checkCode = checkCode(searchDto);
        if(!"success".equals(checkCode)) {
            return R.fail(checkCode);
        }
        //验证日期
        String vildateSearchDate = vildateSearchDate(searchDto.getSearchDate());
        if(!"success".equals(vildateSearchDate)){
            return R.fail(vildateSearchDate);
        }

        return R.success(pdDisasterRecoveryService.selectTQyGuapaifczdList(searchDto));
    }

    /**
     * 线路在线情况明细
     * @param searchDto
     * @return
     * @throws Exception
     */
    @RequestMapping("/lineOnLineDetail")
    public R lineOnLineDetail(@RequestBody SearchDto searchDto) throws Exception {
        String checkCode = checkCode(searchDto);
        if(!"success".equals(checkCode)) {
            return R.fail(checkCode);
        }
        //验证日期
        String vildateSearchDate = vildateSearchDate(searchDto.getSearchDate());
        if(!"success".equals(vildateSearchDate)){
            return R.fail(vildateSearchDate);
        }

        return R.success(pdDisasterRecoveryService.selectTXlZxList(searchDto));
    }
    /**
     * 企业线路在线情况统计
     * @param searchDto
     * @return
     * @throws Exception
     */
    @RequestMapping("/companyOnLineStatistics")
    public R companyOnLineStatistics(@RequestBody SearchDto searchDto) throws Exception {
        String checkCode = checkCode(searchDto);
        if(!"success".equals(checkCode)) {
            return R.fail(checkCode);
        }
        //验证日期
        String vildateSearchDate = vildateSearchDate(searchDto.getSearchDate());
        if(!"success".equals(vildateSearchDate)){
            return R.fail(vildateSearchDate);
        }

        return R.success(pdDisasterRecoveryService.selectTQyZxList(searchDto));
    }


    /**
     * 线路车辆在线情况数据
     * @param searchDto
     * @return
     * @throws Exception
     */
    @RequestMapping("/lineVehicleOnLineData")
    public R lineVehicleOnLineData(@RequestBody SearchDto searchDto) throws Exception {
        String checkCode = checkCode(searchDto);
        if(!"success".equals(checkCode)) {
            return R.fail(checkCode);
        }
        //验证日期
        String vildateSearchDate = vildateSearchDate(searchDto.getSearchDate());
        if(!"success".equals(vildateSearchDate)){
            return R.fail(vildateSearchDate);
        }

        return R.success(pdDisasterRecoveryService.selectTXlClzxList(searchDto));
    }


    /**
     * 企业车辆在线情况数据
     * @param searchDto
     * @return
     * @throws Exception
     */
    @RequestMapping("/companyVehicleOnLineData")
    public R companyVehicleOnLineData(@RequestBody SearchDto searchDto) throws Exception {
        String checkCode = checkCode(searchDto);
        if(!"success".equals(checkCode)) {
            return R.fail(checkCode);
        }
        //验证日期
        String vildateSearchDate = vildateSearchDate(searchDto.getSearchDate());
        if(!"success".equals(vildateSearchDate)){
            return R.fail(vildateSearchDate);
        }

        return R.success(pdDisasterRecoveryService.selectTQyClzxList(searchDto));
    }

    /**
     * 线路车辆出车情况数据
     * @param searchDto
     * @return
     * @throws Exception
     */
    @RequestMapping("/lineVehicleDepartData")
    public R lineVehicleDepartData(@RequestBody SearchDto searchDto) throws Exception {
        String checkCode = checkCode(searchDto);
        if(!"success".equals(checkCode)) {
            return R.fail(checkCode);
        }
        //验证日期
        String vildateSearchDate = vildateSearchDate(searchDto.getSearchDate());
        if(!"success".equals(vildateSearchDate)){
            return R.fail(vildateSearchDate);
        }

        return R.success(pdDisasterRecoveryService.selectTXlClccList(searchDto));
    }


    /**
     * 企业车辆出车情况数据
     * @param searchDto
     * @return
     * @throws Exception
     */
    @RequestMapping("/companyVehicleDepartData")
    public R companyVehicleDepartData(@RequestBody SearchDto searchDto) throws Exception {
        String checkCode = checkCode(searchDto);
        if(!"success".equals(checkCode)) {
            return R.fail(checkCode);
        }
        //验证日期
        String vildateSearchDate = vildateSearchDate(searchDto.getSearchDate());
        if(!"success".equals(vildateSearchDate)){
            return R.fail(vildateSearchDate);
        }

        return R.success(pdDisasterRecoveryService.selectTQyClccList(searchDto));
    }
    /**
     * 线路高峰时段班次配车执行情况
     * @param searchDto
     * @return
     * @throws Exception
     */
    @RequestMapping("/linePeakTimeClassVehicleExecute")
    public R linePeakTimeClassVehicleExecute(@RequestBody SearchDto searchDto) throws Exception {
        String checkCode = checkCode(searchDto);
        if(!"success".equals(checkCode)) {
            return R.fail(checkCode);
        }
        //验证日期
        String vildateSearchDate = vildateSearchDate(searchDto.getSearchDate());
        if(!"success".equals(vildateSearchDate)){
            return R.fail(vildateSearchDate);
        }

        return R.success(pdDisasterRecoveryService.selectTXlGfbcpcList(searchDto));
    }


    /**
     * 线路平均行驶速度表
     * @param searchDto
     * @return
     * @throws Exception
     */
    @RequestMapping("/lineAverageDriveSpeed")
    public R lineAverageDriveSpeed(@RequestBody SearchDto searchDto) throws Exception {
        String checkCode = checkCode(searchDto);
        if(!"success".equals(checkCode)) {
            return R.fail(checkCode);
        }
        //验证日期
        String vildateSearchDate = vildateSearchDate(searchDto.getSearchDate());
        if(!"success".equals(vildateSearchDate)){
            return R.fail(vildateSearchDate);
        }

        return R.success(pdDisasterRecoveryService.selectTXlPingjunCSList(searchDto));
    }
    /**
     * 验证日期
     * @param searchDate
     * @return
     */
    private String vildateSearchDate(String searchDate){
        if(StringUtils.isBlank(searchDate)){
            return "searchDate不能为空";
        }
        if(!searchDate.matches("^\\d{4}\\d{2}\\d{2}$")){
            return "searchDate格式不正确";
        }
        return "success";
    }

    /**
     * 验证code
     * @param searchDto
     * @return
     */
    private String checkCode( SearchDto searchDto){

        //验证非空
        if(StringUtils.isBlank(searchDto.getCode())){
            return "code不能为空";

        }
        //验证无效
        if(!JwtUtil.checkToken(searchDto.getCode())){
            return "code无效或已过期";

        }
        return "success";
    }

//    public static void main(String args[]){
//        String str = "20230416";
//        if(str.matches("^\\d{4}\\d{2}\\d{2}$")){
//            System.out.println("匹配成功");
//        }else{
//            System.out.println("匹配失败");
//        }
//    }
}
